home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH13 / EX13_1B.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-24  |  3.1 KB  |  125 lines

  1. ; EX13_1b.asm
  2. ;
  3. ; This program copies one file to another using blocked I/O.
  4. ; Run this program and time its execution.  Compare the execution time of
  5. ; this program against that of the character at a time I/O and the
  6. ; Standard Library File I/O example (ex13_1a and ex13_1c).
  7.  
  8.         include     stdlib.a
  9.         includelib    stdlib.lib
  10.  
  11.  
  12. dseg        segment    para public 'data'
  13.  
  14. ; File handles for the files we will open.
  15.  
  16. FHndl        word    ?            ;Input file handle
  17. FHndl2        word    ?            ;Output file handle
  18.  
  19. Buffer        byte    256 dup (?)        ;File buffer area
  20.  
  21. FName        equ    this word        ;Ptr to current file name
  22. FNamePtr    dword    FileName
  23.  
  24. Filename    byte    "Ex13_1.in",0        ;Input file name
  25. Filename2    byte    "Ex13_1.out",0        ;Output file name
  26.  
  27. dseg        ends
  28.  
  29.  
  30. cseg        segment    para public 'code'
  31.         assume    cs:cseg, ds:dseg
  32.  
  33. Main        proc
  34.         mov    ax, dseg
  35.         mov    ds, ax
  36.         mov    es, ax
  37.         meminit
  38.  
  39.         mov    ah, 3dh         ;Open the input file
  40.         mov    al, 0            ; for reading
  41.         lea    dx, Filename        ;Presume DS points at filename
  42.         int    21h            ; segment
  43.         jc    BadOpen
  44.         mov    FHndl, ax        ;Save file handle
  45.  
  46.  
  47.         mov    FName, offset Filename2    ;Set this up in case there
  48.         mov    FName+2, seg FileName2    ; is an error during open.
  49.  
  50.         mov    ah, 3ch         ;Open the output file for writing
  51.         mov    cx, 0            ; with normal file attributes
  52.         lea    dx, Filename2        ;Presume DS points at filename
  53.         int    21h            ; segment
  54.         jc    BadOpen
  55.         mov    FHndl2, ax        ;Save file handle
  56.  
  57.  
  58. ; The following loop reads 256 bytes at a time from the file and then
  59. ; writes those 256 bytes to the output file.
  60.  
  61. LP:        mov    ah,3fh            ;Read data from the file
  62.         lea    dx, Buffer        ;Address of data buffer
  63.         mov    cx, 256            ;Read 256 bytes
  64.         mov    bx, FHndl        ;Get file handle value
  65.         int    21h
  66.         jc    ReadError
  67.         cmp    ax, cx            ;EOF reached?
  68.         jne    EOF
  69.  
  70.         mov    ah, 40h            ;Write data to file
  71.         lea    dx, Buffer        ;Address of output buffer
  72.         mov    cx, 256            ;Write 256 bytes
  73.         mov    bx, FHndl2        ;Output handle
  74.         int    21h
  75.         jc    WriteError
  76.         jmp    LP            ;Read next block
  77.  
  78. ; Note, just because the number of bytes read does not equal 256, 
  79. ; don't get the idea we're through, there could be up to 255 bytes 
  80. ; in the buffer still waiting to be processed.
  81.  
  82. EOF:        mov    cx, ax            ;Put # of bytes to write in CX.
  83.         jcxz    EOF2            ;If CX is zero, we're really done.
  84.         mov    ah, 40h            ;Write data to file
  85.         lea    dx, Buffer        ;Address of output buffer
  86.         mov    bx, FHndl2        ;Output handle
  87.         int    21h
  88.         jc    WriteError
  89.  
  90.  
  91. EOF2:        mov    bx, FHndl
  92.         mov    ah, 3eh         ;Close file
  93.         int    21h
  94.         jmp    Quit
  95.  
  96. ReadError:    printf
  97.         byte    "Error while reading data from file '%s'.",cr,lf,0
  98.         dword    FileName
  99.         jmp    Quit
  100.  
  101. WriteError:    printf
  102.         byte    "Error while writing data to file '%s'.",cr,lf,0
  103.         dword    FileName2
  104.         jmp    Quit
  105.  
  106. BadOpen:    printf
  107.         byte    "Could not open '%^s'.  Make sure this file is in the ",cr,lf
  108.         byte    "current directory before attempting to run this program again."
  109.         byte    cr,lf,0
  110.         dword    FName
  111.  
  112. Quit:        ExitPgm                ;DOS macro to quit program.
  113. Main        endp
  114.  
  115. cseg            ends
  116.  
  117. sseg        segment    para stack 'stack'
  118. stk        db    1024 dup ("stack   ")
  119. sseg        ends
  120.  
  121. zzzzzzseg    segment    para public 'zzzzzz'
  122. LastBytes    db    16 dup (?)
  123. zzzzzzseg    ends
  124.         end    Main
  125.